<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<script type="text/javascript" src="${ctx}/js/jquery.itextclear.js"></script>
<script type="text/javascript">
	$(document)
			.ready(
					function() {
						$(
								'input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea',
								'.form').iTextClear();
					});
</script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="CSS/Tree.css" />
<!-- jQplot CSS END -->

<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen" href="lib/datatables/css/vpad.css" />
<!-- DATATABLES CSS END -->

<h1 class="page-title">强制性分布设定 - ${title}</h1>
<div class="container_12 clearfix leading">
	<div class="side grid_12 leading">
		<form class="form has-validation" id="post_form">
			<s:if test="#request.examQualCoerceSet.id > 0">
				<input type="hidden" name="examQualCoerceSet.id"
					value="${examQualCoerceSet.id}" />
			</s:if>
			<input type="hidden" name="examQualCoerceSet.organization.id"
				value="${examQualCoerceSet.organization.id}" /> <input
				type="hidden" name="examQualCoerceSet.type"
				value="${examQualCoerceSet.type}" /> <input type="hidden"
				name="examQualCoerceSet.periodType"
				value="${examQualCoerceSet.periodType}" />
			<div class="clear"></div>
			<div class="clearfix">
				<label for="form-email" class="form-label">绩等 <em>*</em></label>
				<div class="form-input">
					<input type="text" id="name" name="examQualCoerceSet.name"
						value="${examQualCoerceSet.name}" required="required"
						placeholder="请输入绩等" />
				</div>
			</div>
			<div class="clearfix">
				<label for="form-name" class="form-label">分数区间 <em>*</em></label>
				<div class="form-input">
					<input type="text" id="scoreStart"
						name="examQualCoerceSet.scoreStart"
						value="${examQualCoerceSet.scoreStart}" required="required"
						placeholder="请输入0-100的分数下限" /> <input type="text" id="scoreEnd"
						name="examQualCoerceSet.scoreEnd"
						value="${examQualCoerceSet.scoreEnd}" required="required"
						placeholder="请输入0-100的分数上限" />
				</div>
			</div>
			<s:if test="#request.dispatchType=='percentage'">
				<div class="clearfix">
					<label for="form-name" class="form-label">人数比率 <em>*</em></label>
					<div class="form-input">
						<input type="text" id="percentage"
							name="examQualCoerceSet.percentage"
							value='<fmt:formatNumber value="${examQualCoerceSet.percentage*100}" pattern="#.#"></fmt:formatNumber>'
							required="required" placeholder="请输入0-100的百分比" />
					</div>
				</div>
			</s:if>
			<s:elseif
				test="#request.examQualCoerceSet.type.toString() == 'percentage'">
				<div class="clearfix">
					<label for="form-name" class="form-label">人数比率 <em>*</em></label>
					<div class="form-input">
						<input type="text" id="percentage"
							name="examQualCoerceSet.percentage"
							value='<fmt:formatNumber value="${examQualCoerceSet.percentage*100}" pattern="#.#"></fmt:formatNumber>'
							required="required" placeholder="请输入0-100的百分比" />
					</div>
				</div>
			</s:elseif>
			<s:else>
				<div class="clearfix">
					<label for="form-name" class="form-label">分配人数 <em>*</em></label>
					<div class="form-input">
						<input type="text" id="number" name="examQualCoerceSet.number"
							value="${examQualCoerceSet.number}" required="required"
							placeholder="请输入人数" />
					</div>
				</div>
			</s:else>
			<div class="clearfix">
				<label class="form-label" for="form-textarea">备注</label>
				<div class="form-input form-textarea">
					<span class="iTextClearButtonContainer"><textarea
							placeholder="备注" rows="5" id="form-textarea"
							name="examQualCoerceSet.remark">${examQualCoerceSet.remark}</textarea><a
						class="iTextClearButton" style="display: none;"></a></span>
				</div>
			</div>
			<div class="form-action clearfix">
				<button class="button" type="button" onclick="checkForm()">
					<s:if test="#request.examQualCoerceSet.id > 0">修改</s:if>
					<s:else>添加</s:else>
				</button>
				<button class="button" type="reset">重置</button>
				<a class="button" href="javascript:void(0)"
					onclick="javascript:history.back()"><span
					style="background: url(${ctx}/images/img/arrow_undo.png)"
					class="icon">&nbsp;</span>返回</a>
			</div>
		</form>
	</div>
</div>
<div class="clear"></div>
<script type="text/javascript" src="js/Tree.js"></script>
<script type="text/javascript">
	function checkForm() {
		if ($("#name").val() == null) {
			alert("请填写绩等");
			return;
		}
		if ($("#scoreStart").val() == null
				|| $("#scoreStart").val().trim() == ""
				|| isNaN($("#scoreStart").val())
				|| parseFloat($("#scoreStart").val().trim()) < 0
				|| parseFloat($("#scoreStart").val().trim()) > 100) {
			alert("请输入0-100的分数下限");
			return;
		}
		if ($("#scoreEnd").val() == null || $("#scoreEnd").val().trim() == ""
				|| isNaN($("#scoreEnd").val())
				|| parseFloat($("#scoreEnd").val().trim()) < 0
				|| parseFloat($("#scoreEnd").val().trim()) > 100) {
			alert("请输入0-100的分数上限");
			return;
		}

		if (($("#percentage").val() == null
				|| $("#percentage").val().trim() == ""
				|| Number($("#percentage").val().trim()) < 0 || Number($(
				"#percentage").val().trim()) > 100)
				&& ($("#number").val() == null
						|| $("#number").val().trim() == "" || Number($(
						"#number").val().trim()) < 0)
				&& (isNaN($("#number").val()) || isNaN($("#number").val()))) {
			alert("请填写百分比或分配人数");
			return;
		}

		if (parseFloat($("#scoreStart").val().trim()) >= parseFloat($(
				"#scoreEnd").val().trim())) {
			alert("强制分页分数区间设定异常，下限不能大于上限,请重新输入.");
			return;
		}
		var p = $("#percentage").val();
		var sp = ${percentage};
		if (p > sp || p < 0) {
			alert("请输入0~" + sp + "之间的数")
			$("#percentage").attr("value", "");
			return false;
		}

		//检测数据有效性
		var url = "${ctx}/exam/validate_score.xhtml";
		var availability = true;
		$.ajax(
			url,
			{
				type : "POST",
				async : false,
				data : {
					"organizationId" : eval("${examQualCoerceSet.organization.id}"),
					"st" : parseFloat($("#scoreStart").val().trim()),
					"se" : parseFloat($("#scoreEnd").val().trim()),
					"i" : eval("${examQualCoerceSet.id}"),
					"periodType" : "${examQualCoerceSet.periodType}"
				},
				dataType : "html",
				cache : false,
				success : function(data) {
					if (data != null) {
						var validate = eval("(" + data + ")");
						if (!validate.availability) {
							availability = false;
						}
					}
				},
				error : function(XMLHttpRequest, textStatus,
						errorThrown) {
					alert('检测数据异常');
				},
				complete : function() {
				}
			});
		if (!availability) {
			alert("输入的分数区间已经存在，请重新输入");
			return;
		}
		var url = "${ctx}/exam/post_qualset.xhtml";
		$.ajax(url, {
			type : "POST",
			data : $("#post_form").serialize(),
			dataType : "html",
			cache : false,
			success : function(a) {
                window.location.href = "${ctx}/index.xhtml#exam/qualset_main.xhtml";
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert('提交申请发生异常');
			},
			complete : function() {
			}
		});
	}
</script>
